import util from "../../util";
const api = util.api.Schedule  // Schedule
export default class scheduleModel {
    constructor() {
        this.util=util;
        this.filter=null;
        this.record={};
        this.pointer=null;
    }
    setRecord(record){
        if(!!record.name){
            this.record=record;
            this._setFilter();
        }else{
            this.record={};
            this._setFilter(false);
        }
    }
    static create(record){
        let object=new scheduleModel();
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    _getFilter(array){
        let filter='';
        if(util.tools.testType(array)=='array'){
            filter= `category='${array[0]}'&&name='${array[1]}'`
        }else if(util.tools.testType(array)=='object'){
            filter= `category='${array.category}'&&name='${array.name}'`
        }else if(util.tools.testType(array)=='string'&&array.split(',').length==2){
            let arr=array.split(',');
            filter= `category='${arr.category}'&&name='${arr.name}'`
        }else{
            filter= array;
        }
        filter=`${filter}`
        return filter;
    }
    _setFilter(empty=true){
        if(!empty){
            this.filter=null;
            this.pointer=null;
            return;
        }
        this.filter=[this.record.category,this.record.name];
        this.pointer={
            category:this.record.category,
            name:this.record.name,
            id:this.record.id,
            version:this.record.version,
            published:this.record.published,
        };
    }
    /*******************************************************************************
     * 对象属性编辑功能
     * ******************************************************************************/
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    async add(record){
        if(!record || !record.name){
            return false;
        }
        let newRecord=Object.assign(scheduleModel.tpl(),record);
        let result = await api.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 删除
     * @returns {Promise<boolean|*>}
     */
    async delete(){
        if(!!this.record.id){
            let result = await api.delete(this.record.id).then(flag=>{
                if(flag){
                    this.setRecord([]);
                    return true;
                }else{
                    return false;
                }
            }).catch(err=>{
                return false;
            });
            return result;
        }else{
            return false;
        }
    }
    /**
     * 更新
     * @returns {Promise<*>}
     */
    async update(){
        let result = await api.update(this.record).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 启动、停止
     * @returns {Promise<*>}
     */
    async quartz(){
        let result = await api.quartz(this.record).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 查询
     * @param param
     * @returns {Promise<*>}
     */
    async query(category,name){
        let filter=`category='${category}'&&name='${name}'`;
        let result = await api.queryIncludeUnpublished({condition:filter}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 新增模板
     * @returns {{comments: string, web: {}, values: {}, name: string, className: string, published: boolean, type: string, version: number, hier: *[], others: {}}}
     */
    static tpl(){
        return {
            name: '',
            eventCategory: '',
            eventName: '',
            eventParam: {},
            // className: 'schedule',
            cronExpression: '',
            description: '',
            status: 0,
            // features: [],
        }
    }
}